//
//  XHItemsScrollView.m
//  XiangHa_3
//
//  Created by xia on 15/3/30.
//  Copyright (c) 2015年 xiangha. All rights reserved.
//

#import "XHItemsScrollView.h"
//#define normalColor @"0f0f0f"
//#define selectedColor @"2665e4"
//#define lineBackClor [UIColor colorWithHexString:@"2665e4"]
#define cnormalColor @"ffffff"
#define cselectedColor @"ffffff"
#define lineBackClor [UIColor colorWithHexString:@"ffffff"]
@interface XHItemsScrollView()
{
    float _btnSpace;
    UIView *_maskView;
    int _currentIndex;
    NSArray *_dataArray;
    float _startX;
    UIColor *normalColor;
    UIColor *selectedColor;
    NSInteger type;
}
@end

@implementation XHItemsScrollView
+(XHItemsScrollView *)createItemsScrollViewWithdelegate:(id)clickDelegate withSpace:(CGFloat)space{
    XHItemsScrollView *itemsScrollView=[[XHItemsScrollView alloc]initWithFrame:CGRectMake(space, 0, [UIScreen mainScreen].bounds.size.width - space * 2, selfHeight)];
    itemsScrollView.backgroundColor = [UIColor blackColor];
    itemsScrollView.clickDelegate=clickDelegate;
    return itemsScrollView;
}

-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        normalColor = [UIColor colorWithHexString:cnormalColor];
        selectedColor = [UIColor colorWithHexString:cselectedColor];
        _enableAlignCenter=YES;
        _enableLeftMargin=YES;
        _startX=0;
        self.backgroundColor=[UIColor whiteColor];
        self.tag=1000;
        self.showsHorizontalScrollIndicator=NO;
        self.showsVerticalScrollIndicator=NO;
        _topBtnArray=[[NSMutableArray alloc]initWithCapacity:0];
        _btnSpace = 30 ;
        
//        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, self.height - 0.5, [UIScreen mainScreen].bounds.size.width, 0.5)];
//        line.backgroundColor = [UIColor colorWithHexString:@"f1f1f1"];
//        [self addSubview:line];
    }
    return self;
}
-(void)setItemsWithDataArray:(NSArray *)dataArray withDirection:(int)direction{
    if (!_enableLeftMargin) {
        _startX=0;
    }
    //_startX = 22.5;
    _dataArray=dataArray;
    _lineViewsArray=[[NSMutableArray alloc]initWithCapacity:0];
    UIButton *lastBtn=nil;
    float sumWidth=0;

    for(int i=0;i<dataArray.count;i++){
        NSDictionary *dict=[dataArray objectAtIndex:i];
        NSString *name=[dict objectForKey:@"title"];
        NSString *tempName = LocalizedString(name);
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor=[UIColor clearColor];
        [btn setTitle:tempName forState:UIControlStateNormal];
        btn.titleLabel.font= MY_SCALE_RegularFONT(14);
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.tag=1000+i;
        [btn addTarget:self action:@selector(topBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        CGSize size=[name sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}];
        if (lastBtn) {
//            btn.frame=CGRectMake(CGRectGetMaxX(lastBtn.frame)+_btnSpace, 0, size.width, self.height - 0.5);
            btn.frame=CGRectMake(CGRectGetMaxX(lastBtn.frame), 0, size.width +_btnSpace , self.height - 10);
        }else{
            btn.frame=CGRectMake(_startX, 0, size.width +_btnSpace, self.height - 10);
        }
        [_topBtnArray addObject:btn];
        [btn setTitleColor:normalColor forState:UIControlStateNormal];
        sumWidth += btn.width;

        UIView *view=[[UIView alloc]initWithFrame:CGRectMake((btn.width - 20) / 2, btn.height - 4, 20, 3)];
        if (direction == 1) {
            view.top = 0;
        }
        view.tag=2000;
        view.layer.cornerRadius = 1.5;
        [btn addSubview:view];
        view.backgroundColor = ThemeColor;
        lastBtn=btn;
    }
    
    if (!_enableAlignCenter) {
        [self setContentSize:CGSizeMake(CGRectGetMaxX(lastBtn.frame)+_startX, self.height)];
        return;
    }

    if (sumWidth < self.width) {
        CGFloat btnWidth = self.width / dataArray.count;

        for(int i=0;i<_topBtnArray.count;i++) {
            UIButton *btn=_topBtnArray[i];
            if (i==0) {
                btn.frame=CGRectMake(0, 0, btnWidth, self.height-0.5);
            }
            else{
                btn.frame=CGRectMake(lastBtn.right, 0, btnWidth, self.height-0.5);
            }
            UIView *subView=[btn viewWithTag:2000];
            subView.centerX = btn.width / 2;
            lastBtn=btn;
        }
        return;
    }

    float firstBtnX=_startX;
    if (CGRectGetMaxX(lastBtn.frame)<self.width-_startX*2) {
        firstBtnX=(self.width-(CGRectGetMaxX(lastBtn.frame)-_startX))/2;
    }
    else{
        [self setContentSize:CGSizeMake(CGRectGetMaxX(lastBtn.frame)+15, self.height)];

    }
    
//    for(int i=0;i<_topBtnArray.count;i++) {
//        UIButton *btn=_topBtnArray[i];
//        if (i==0) {
//            btn.frame=CGRectMake(firstBtnX, 0, btn.width, self.height-10);
//        }
//        else{
//            btn.frame=CGRectMake(CGRectGetMaxX(lastBtn.frame)+_btnSpace, 0, btn.width, self.height-10);
//
//        }
//        //UIView *subView=[btn viewWithTag:2000];
//        //subView.left = -_btnSpace/2;
//        lastBtn=btn;
//    }


}
-(void)topBtnClick:(UIButton *)btn{
    int index=(int)btn.tag-1000;
    if ([self.clickDelegate respondsToSelector:@selector(XHItemsScrollViewClickWithIndex:)]) {
        [self.clickDelegate XHItemsScrollViewClickWithIndex:index];
    }
}

- (void)setNormalColor:(NSString *)normalC withSelectColor:(NSString *)selectC{
    normalColor = [UIColor colorWithHexString:normalC];
    selectedColor = [UIColor colorWithHexString:selectC];
    [self setItemsToIndex:0];
}

-(void)yuanType:(NSInteger)num{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height + 15)];
    view.backgroundColor = UIColor.clearColor;
    view.layer.cornerRadius = 15;
    [self addSubview:view];
    [self sendSubviewToBack:view];
    self.backgroundColor = UIColor.clearColor;
    type = num;
    CGFloat left = 0;
    for (UIButton *btn in _topBtnArray) {
        btn.width = btn.width + 10;
        btn.left = left;
        btn.height = self.height / 2;
        btn.centerY = self.height / 2;
        btn.layer.cornerRadius = btn.height / 2;
        [[btn viewWithTag:2000] removeFromSuperview];
        left = left + btn.width + 5;
    }
    [self setContentSize:CGSizeMake(left + 15, self.height)];
    [self setItemsToIndex:0];
}

-(void)setItemsToIndex:(int)index{
    __block float fx = 0;
    int preIndex=_currentIndex;
    _currentIndex = index;
    if (_topBtnArray.count>0) {
        UIButton *button = [_topBtnArray objectAtIndex:index];
        int dirction = index > preIndex ? 1 : -1;

        fx  = button.center.x - self.contentOffset.x - self.width / 2;

        [UIView animateWithDuration:0.15 animations:^{
            if (dirction == 1 && button.center.x - self.contentOffset.x > self.width / 2 ) {
                fx = MIN(self.contentOffset.x + fx, self.contentSize.width - self.width);
                if (fx < 0) {
                    fx = 0;
                }
                [self setContentOffset:CGPointMake(fx,0)];


            } else if (dirction == -1 && button.center.x - self.contentOffset.x < self.width / 2){
                fx = MAX(self.contentOffset.x + fx, 0);
                [self setContentOffset:CGPointMake(fx,0)];
            }

        }];
        
        if (type == 1) {
            for (int i = 0; i < _topBtnArray.count; i++) {
                UIButton *button =_topBtnArray[i];
                if (i == index) {
                    button.backgroundColor = MYRed;
                    [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
                    button.titleLabel.font= MY_SCALE_FONT(14);
                } else {
                    button.backgroundColor = UIColor.whiteColor;
                    [button setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
                    button.titleLabel.font= MY_SCALE_FONT(13);
                }
            }
        }else{
            for (int i = 0; i < _topBtnArray.count; i++) {
                UIButton *button =_topBtnArray[i];
                UIView *subView=[button viewWithTag:2000];
                if (i == index) {
                    subView.hidden = NO;
                    [button setTitleColor:selectedColor forState:UIControlStateNormal];
                    button.titleLabel.font= MY_SCALE_SemiboldFONT(18);
//                    subView.backgroundColor = TemplateColor ;

                } else {
//                    subView.backgroundColor = TemplateColor;
                    subView.hidden = YES;
                    button.titleLabel.font = MY_SCALE_RegularFONT(14);
                    [button setTitleColor:normalColor forState:UIControlStateNormal];
                }
            }
        }
        
    }
}

@end
